import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './assets/css/style.css'
import axios from "axios";
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// 引入中文包
import zhCn from "element-plus/es/locale/lang/zh-cn";
import moment from "moment";

//引入echarts
import * as echarts from 'echarts';



const app = createApp(App)
//图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}



axios.defaults.baseURL = 'http://127.0.0.1:8086/';
axios.interceptors.request.use(config=>{
    //设置请求头
    config.headers={
        "token": sessionStorage.getItem("token"),
    }
    // console.log("token=>"+window.sessionStorage.getItem("token"))
    return config;
})

let msg = false;
axios.interceptors.response.use(response => {
    // 对响应数据做点什么
    //     console.log("===拿到数据了===")
    msg=false
    return response
}, error => {
    if(error.response.status===401){
        router.push('/login').catch(() => {});// 跳转到登录页面
        if(msg==false){
            alert("登录过期！");
            msg=true
        }
        return new Promise(() => {});
    }
    return Promise.reject(error);
})

app.config.globalProperties.$http=axios
//挂在echarts
app.config.globalProperties.$echarts=echarts;
//全局过滤器
app.config.globalProperties.$moment = moment; // 将moment绑定到全局属性
app.config.globalProperties.$filters={
    formatDate(value) {
        return app.config.globalProperties.$moment(value).format('YYYY-MM-DD HH:mm:ss');
    }
}

app.use(ElementPlus,{
    locale: zhCn,
})
app.use(store)
app.use(router)
app.mount('#app')
